﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:sp="http://schemas.microsoft.com/sharepoint/"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl sp"
                extension-element-prefixes="sp">
  <xsl:output method="xml" indent="yes"/>

  <!--Imports-->
  <xsl:include href="elements.xslt"/>

  <!--
  ========================================================================================
  Point d'entrée : Noeud Feature à la racine
  ========================================================================================
  -->
  <xsl:template match="sp:Feature">

    <xsl:param name="featureFolder"/>

    <Feature>
      <xsl:copy-of select="@Scope"/>
      <xsl:copy-of select="@Id"/>
      <xsl:copy-of select="@Title"/>

      <ActivationDependencies>
        <xsl:apply-templates select="sp:ActivationDependencies/sp:ActivationDependency"/>
      </ActivationDependencies>

      <FeatureElements>
        <xsl:apply-templates select="sp:ElementManifests/sp:ElementManifest">
          <xsl:with-param name="featureFolder" select="$featureFolder"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="sp:ElementManifests/sp:ElementFile">
          <xsl:with-param name="featureFolder" select="$featureFolder"/>
        </xsl:apply-templates>
      </FeatureElements>

    </Feature>
  </xsl:template>

  <!--
  ========================================================================================
  Template [ActivationDependency]
  ========================================================================================
  -->
  <xsl:template match="sp:ActivationDependency">
    <ActivationDependency>
      <xsl:copy-of select="@FeatureId"/>
      <xsl:copy-of select="@FeatureTitle"/>
    </ActivationDependency>
  </xsl:template>

  <!--
  ========================================================================================
  Template [ElementManifest]
  ========================================================================================
  -->
  <xsl:template match="sp:ElementManifest">


    <xsl:param name="featureFolder"/>

    <xsl:variable name="extension" select="substring(@Location, string-length(@Location) - 3)"/>
    <xsl:variable name="elementFolder" select="substring-before(@Location, '\')"/>
    <xsl:variable name="elementPath" select="concat($featureFolder,'\', @Location)"/>

    <xsl:if test="$extension = '.xml'">

      <!--fichier lié-->
      <xsl:variable name="elementManifest" select="document($elementPath)"/>

      <!--Transformation-->
      <xsl:apply-templates select="$elementManifest/sp:Elements">
        <xsl:with-param name="featureFolder" select="$featureFolder"/>
        <xsl:with-param name="elementFolder" select="$elementFolder"/>
      </xsl:apply-templates>

    </xsl:if>

  </xsl:template>

  <!--
  ========================================================================================
  Template [ElementFile]
  ========================================================================================
  -->
  <xsl:template match="sp:ElementFile">

    <xsl:param name="featureFolder"/>

    <xsl:variable name="extension" select="substring(@Location, string-length(@Location) - 3)"/>
    <xsl:variable name="elementFolder" select="substring-before(@Location, '\')"/>
    <xsl:variable name="elementPath" select="concat($featureFolder,'\', @Location)"/>

    <xsl:if test="$extension = '.xml'">

      <!--fichier lié-->
      <xsl:variable name="elementFile" select="document($elementPath)"/>

      <!--Transformation pour les définitions de liste-->
      <xsl:apply-templates select="$elementFile/sp:List">
        <xsl:with-param name="featureFolder" select="$featureFolder"/>
        <xsl:with-param name="elementFolder" select="$elementFolder"/>
      </xsl:apply-templates>

    </xsl:if>

  </xsl:template>
</xsl:stylesheet>
